package com.lmind.support.etc;

import java.util.concurrent.atomic.AtomicLong;

public class Interval {
	
	private AtomicLong old;
	
	private AtomicLong value;
	
	private AtomicLong interval;

	public Interval(long lastValue, long interval) {
		this.old = new AtomicLong(lastValue);
		this.value = new AtomicLong(lastValue);
		this.interval = new AtomicLong(interval);
	}

	public long getLastValue() {
		return value.get();
	}

	public long getInterval() {
		return interval.get();
	}
	
	public long getDuration() {
		return value.get() - old.get();
	}
	
	public boolean check(long newValue) {
		if (value.get() + interval.get() < newValue) {
			old.set(value.get());
			value.set(newValue);
			return true;
		} else {
			return false;
		}
	}
	
	public void refresh(long newValue) {
		old.set(value.get());
		value.set(newValue);
	}
}
